<?php
namespace Util;
/**
* validate
*/
final class Validate
{
    const EMAIL = '/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/';
    const PHONE = '/^((010|02[0-9]|0[3-9][0-9]{2})-)?(\d){7,9}(-\d+)?$/';
    const MOBILE = '/^1[3|4|5|7|8]{1}\d{9}$/';
    const QQ = '/^[1-9]([0-9]{5,11})$/';
    const ACCOUNT = '/^[a-zA-Z0-9_]{6,20}$/';

    /**
     * test 验证方法
     * @param  $key
     * @param  $str
     * @return BOOL
     * @date   2016-11-08
     */
    public static function test($key, $str)
    {
        $key = strtoupper((string)$key);
        $const = "self::$key";
        if(empty($key) || !defined($const)) {
            return false;
        }

        return preg_match(constant($const), $str) ? true : false;
    }

    /**
     * 获取单个正则
     * @param  string     $key
     * @return string
     * @author imjcw
     * @date   2016-11-14
     */
    public static function getPattern($key)
    {
        $key = strtoupper((string)$key);
        $const = "self::$key";
        if (!defined($const)) {
            return false;
        }
        return constant($const);
    }

    /**
     * 获取所有定义的正则
     * @return array
     * @date   2016-11-08
     */
    public static function getPatterns()
    {
        $oClass = new \ReflectionClass(__CLASS__);
        return $oClass->getConstants();
    }
}